//
//  ChoiceMoveCardViewController.m
//  carcareIOS
//
//  Created by 王然 on 16/9/19.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "ChoiceMoveCardViewController.h"
#import "carcareIOS-Swift.h"

@interface ChoiceMoveCardViewController ()

//获取随机样式使用的url
@property (nonatomic , strong) MoveCarQRModel * moveCarQRModel;

@end

@implementation ChoiceMoveCardViewController

-(void)dealloc{
    self.moveCarQRModel = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择挪车卡";
    
    [self.view addSubview:self.headBlackV];
    
    self.view.backgroundColor = COLOR_HEXSTRING(@"#272727");
    [self createUI];
}
- (void)reloadMoveCarQRModel:(MoveCarQRModel *)moveCarQRModel upDateStyleId:(void(^)(NSNumber * styleID))reloadDate
{
    [self loadChoiceCarDataWithModel:moveCarQRModel upDateStyleId:reloadDate];
}
- (void)createUI
{
    CGFloat imageVY = 27 * __MAIN_RATIO_375;
    CGFloat imageVX = 37 * __MAIN_RATIO_375;
    CGFloat imageVH = 386 * __MAIN_RATIO_375;
    self.imageV = [[UIImageView imageViewWithFrame:CGRectMake(imageVX, imageVY, __MAIN_WIDTH - imageVX * 2, imageVH) image:nil] setupOnView:self.view];
    self.imageV.backgroundColor = [UIColor whiteColor];
    NSArray * iconAndTexts = @[@[@"choiceMoveCardChange",@"choiceMoveCardSure"],@[@"换一张",@"确定"]];
    
    CGFloat iconX = 80 * __MAIN_RATIO_375;
    __weak __typeof(self) wself = self;
    for (int index = 0; index < iconAndTexts.count; index ++) {
        UIImage * image = ImageNamed(iconAndTexts[0][index]);
        [MineMoveCardBtnV text:iconAndTexts[1][index] iconName:iconAndTexts[0][index] index:index onView:self.view frame:CGRectMake( iconX + (image.size.width + (__MAIN_WIDTH - (image.size.width + iconX )* 2)) * index, self.imageV.bottom + 75 * __MAIN_RATIO_375, image.size.width, image.size.height + 13 * __MAIN_RATIO_375 + 19) click:^(NSInteger index) {
            [wself clickOnChangeOrSurebtn:index];
        }];
    }
}
//获取挪车卡使用的样式
- (void)loadChoiceCarDataWithModel:(MoveCarQRModel *)model upDateStyleId:(void(^)(NSNumber * styleID))reloadDate
{
    self.moveCarQRModel = model;
    __weak __typeof(self) wself = self;
    
    [[MoveCarManager sharedInstance] randomQRTypeWithQrURL:model.tdCodeUrl success:^(MoveCarTypeQRModel * _Nullable moveCarTYpeQRModel) {
        wself.moveCarQRModel.styleId = moveCarTYpeQRModel.id;
        if(reloadDate)reloadDate(wself.moveCarQRModel.styleId);
        if(moveCarTYpeQRModel.showQRImage)wself.image = moveCarTYpeQRModel.showQRImage;
    } fail:^{
        
    }];
    
}
//点击 选择挪车卡页面 换一张 或者 确定  按钮
- (void)clickOnChangeOrSurebtn:(NSInteger)index
{
    
    switch (index) {
        case 0://换一张
        {
            [self loadChoiceCarDataWithModel:self.moveCarQRModel upDateStyleId:nil];
        }
            break;
        case 1://确定
        {
            __weak __typeof(self) wself = self;
            
            [NetMoveCar updateQRWithModel:self.moveCarQRModel success:^
             {
                 [[MoveCarManager sharedInstance] updateQRListWithSuccess:^{
                 } fail:^{
                 }];
                 NSArray *viewControllers = wself.navigationController.viewControllers;
                 for (NSInteger i = viewControllers.count - 1; i >= 0; i--) {
                     UIViewController *vc = viewControllers[i];
                     if ([vc isKindOfClass:[MineMoveCardViewController class]]) {
                         [wself.navigationController popToViewController:vc animated:YES];
                         break;
                     }
                 }
             } fail:^{
             }];
            
        }
            break;
        default:
            break;
    }
}

- (void)setImage:(UIImage *)image
{
    _image = image;
    if(image)[self.imageV setImage:image];
}
- (UIView *)headBlackV
{
    if (!_headBlackV) {
        _headBlackV = [[UIView viewWithFrame:CGRectMake(0, -64, __MAIN_WIDTH, 70)] resetBackgroundColor:COLOR_HEXSTRING(@"#272727")];
    }
    return _headBlackV;
}
#pragma mark - nav
-(UIColor *)navBackgroundColor{
    return [UIColor clearColor];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
